import { onMounted } from "vue";

function useBase64(option) {
  // 图片url转换成base64
  return new Promise((resolve)=>{
    onMounted(()=>{
      let img = document.querySelector(option.el)
      img.onload = () =>{ // 图片加载完
        resolve({
          baseUrl:useBase64(img)
        })
      }
    })
  
    const useBase64 = (el)=>{
      const canvas = document.createElement('canvas')
      const ctx = canvas.getContext('2d')
      canvas.width = el.width
      canvas.height = el.height
      ctx?.drawImage(el, 0, 0, canvas.width, canvas.height)
      return canvas.toDataURL('image/png')
    }
  })
}
export default useBase64